<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="title">物流仓储管理系统</div>
      <el-tabs v-model="activeTab" @tab-click="handleClick">
        <el-tab-pane label="登录" name="login">
          <el-form :model="loginForm" :rules="loginRules" ref="loginForm" label-width="0">
            <el-form-item prop="username">
              <el-input 
                v-model="loginForm.username" 
                prefix-icon="el-icon-user"
                placeholder="请输入用户名"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input 
                v-model="loginForm.password" 
                prefix-icon="el-icon-lock"
                type="password"
                placeholder="请输入密码"
                @keyup.enter.native="handleLogin"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" style="width: 100%" @click="handleLogin">
                登 录
              </el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        
        <el-tab-pane label="注册" name="register">
          <el-form :model="registerForm" :rules="registerRules" ref="registerForm" label-width="0">
            <el-form-item prop="username">
              <el-input 
                v-model="registerForm.username" 
                prefix-icon="el-icon-user"
                placeholder="请输入用户名"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input 
                v-model="registerForm.password" 
                prefix-icon="el-icon-lock"
                type="password"
                placeholder="请输入密码"
              />
            </el-form-item>
            <el-form-item prop="confirmPassword">
              <el-input 
                v-model="registerForm.confirmPassword" 
                prefix-icon="el-icon-lock"
                type="password"
                placeholder="请确认密码"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" style="width: 100%" @click="handleRegister">
                注 册
              </el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  name: 'LoginName',
  data() {
    // 密码确认验证
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      activeTab: 'login',
      loginForm: {
        username: '',
        password: ''
      },
      registerForm: {
        username: '',
        password: '',
        confirmPassword: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 1, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, message: '密码不能为空', trigger: 'blur' }
        ]
      },
      registerRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleClick() {
      this.$refs.loginForm?.resetFields()
      this.$refs.registerForm?.resetFields()
    },
    
    // 处理登录 - 使用 params 方式发送请求，匹配后端 @RequestParam
    async handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          try {
            const res = await request({
              url: '/user/login',
              method: 'post',
              params: {
                username: this.loginForm.username.trim(),
                password: this.loginForm.password.trim()
              }
            })
            if (res.code === 200) {
              sessionStorage.setItem('user', JSON.stringify(res.data))
              this.$message.success('登录成功')
              this.$router.push('/')
            }
          } catch (error) {
            console.error(error)
            this.$message.error(error.response?.data?.msg || '登录失败')
          }
        }
      })
    },
    
    // 处理注册 - 使用 data 方式发送请求，匹配后端 @RequestBody
    async handleRegister() {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          try {
            const { username, password } = this.registerForm
            const res = await request({
              url: '/user/register',
              method: 'post',
              data: { 
                username: username.trim(), 
                password: password.trim() 
              }
            })
            if (res.code === 200) {
              this.$message.success('注册成功，请登录')
              this.activeTab = 'login'
              this.$refs.registerForm.resetFields()
            }
          } catch (error) {
            this.$message.error(error.response?.data?.msg || '注册失败')
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #2d3a4b;
}

.login-card {
  width: 400px;
  padding: 20px;
}

.title {
  font-size: 26px;
  font-weight: bold;
  color: #303133;
  text-align: center;
  margin-bottom: 30px;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input {
  height: 40px;
}
</style> 